package com.mystnihon.tdroid.loader;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


import android.content.Context;
import android.net.Uri;
import ca.benow.transmission.TransmissionClient;
import ca.benow.transmission.model.AddedTorrentInfo;

public class AddTorrentLoader extends AbstractAsyncLoader<AddedTorrentInfo> {


	Uri mUri;
	TransmissionClient mClient;

	public AddTorrentLoader(TransmissionClient client, Uri uri) {
		mClient = client;
		mUri = uri;
		// mIsFile = isFile;

	}

	@Override
	protected boolean doLoad(Context context) {

		try {
			if (mUri.getScheme().equals("file")) {
				InputStream in = context.getContentResolver().openInputStream(mUri);
				AddedTorrentInfo data = mClient.addTorrent(null, in, false, -1, -1, null, null, null, null, null);
				setDataObject(data);

			} else {
				setDataObject(mClient.addTorrent(null, mUri.toString(), false, -1, -1, null, null, null, null, null));
			}
			return true;

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
